

function StatusBarManager() {
}

StatusBarManager.prototype.kwStatusPanel = null; 
StatusBarManager.prototype.kwButtonStatusDisabled = "disabled"; //off
StatusBarManager.prototype.kwButtonStatusOff = "off"; //off
StatusBarManager.prototype.kwButtonStatusOn = "on"; //on
StatusBarManager.prototype.kwInterval = 700; //half second
StatusBarManager.prototype.kwFlashId = null;
StatusBarManager.prototype.kwFlashCount = 0;
StatusBarManager.prototype.kwFlashNumber = 0;  //number of requested flashes
StatusBarManager.prototype.pmStatusPanel = null; 
StatusBarManager.prototype.lrStatusPanel = null; 
StatusBarManager.prototype.saleStatusPanel = null; 
StatusBarManager.prototype.saleIntervalId = null;
StatusBarManager.prototype.saleInterval = 5000;
StatusBarManager.prototype.defaultSaleInterval = 5000;
StatusBarManager.prototype.oldState = null;
StatusBarManager.prototype.saleShowPercentOff = false;


StatusBarManager.prototype.flashKeywordButton = function(numberOfFlashes) {
	//alert('flashKeywordButton entered ' + this.kwFlashId );
	if (numberOfFlashes == null) numberOfFlashes = -1;
	
	if (this.kwFlashId != null) {
		this.kwFlashCount = 0;
		this.kwFlashNumber = numberOfFlashes;
		//another event to flash, result counter
		//clearInterval(this.kwFlashId);
		//this.kwStatusPanel.setAttribute("status", handle.kwButtonStatusOff);
	}
	else {

		this.kwFlashNumber = numberOfFlashes;
		//alert('flashKeywordButton entered ' + this.kwFlashId );
		this.kwFlashCount = 0;
		this.kwFlashId = setInterval(this.submitFlashKeyword, this.kwInterval, this);
		//alert('flashKeywordButton entered ' + this.kwFlashId );
		this.kwStatusPanel.tooltipText='Look Who\'s Talking';
	}
}

StatusBarManager.prototype.submitFlashKeyword = function(handle) {
	if (handle == null) return;
	if (handle.kwStatusPanel == null) return;
	var kwbstatus = (handle.kwStatusPanel.getAttribute("status") == handle.kwButtonStatusOn)? handle.kwButtonStatusOff: handle.kwButtonStatusOn;
	handle.kwStatusPanel.setAttribute("status", kwbstatus);
	if (handle.kwFlashNumber != -1) {
		

		if (handle.kwFlashCount >= handle.kwFlashNumber) {
			//alert(handle.kwFlashCount + ' clear flash ' + numFlashes);
			clearInterval(handle.kwFlashId);
			handle.kwFlashId = null;
			handle.kwStatusPanel.setAttribute("status", handle.kwButtonStatusOff);
			handle.kwStatusPanel.tooltipText='Chat Connected';
			handle.kwFlashCount = 0;
			handle.kwFlashNumber = 0;
		}
		if (kwbstatus == handle.kwButtonStatusOn) handle.kwFlashCount++;
	}
			
}

StatusBarManager.prototype.setKWStatusBarPanel = function(statusBarPanel)
{
 this.kwStatusPanel = statusBarPanel;
 //if (this.kwStatusPanel == null) return;
 //this.kwStatusPanel.setAttribute("status", this.kwButtonStatusOff);
}

StatusBarManager.prototype.setKWStatusConnected = function()
{
 if (this.kwStatusPanel == null) return;
 	this.kwStatusPanel.setAttribute("status", this.kwButtonStatusOff);
 	this.kwStatusPanel.tooltipText='Chat Connected';
}

StatusBarManager.prototype.setKWStatusDisabled = function()
{
 	if (this.kwStatusPanel == null) return;
 	if (this.kwFlashId != null) {
 		clearInterval(this.kwFlashId);
 	}
  this.kwFlashId = null;
  this.kwFlashCount = 0;
	this.kwFlashNumber = 0;
	this.kwStatusPanel.setAttribute("status", this.kwButtonStatusDisabled);
	this.kwStatusPanel.tooltipText='Chat Disconnected';
}

StatusBarManager.prototype.isKeywordButtonFlashing = function() {
	var flash = this.kwFlashId  == null ? false : true;
	return flash;
}

StatusBarManager.prototype.setPMStatusBarPanel = function(statusBarPanel)
{
 this.pmStatusPanel = statusBarPanel;
 if (this.pmStatusPanel == null) return;
 //this.pmStatusPanel.setAttribute("status", "on");
}

StatusBarManager.prototype.setPMStatusOn = function()
{
 if (this.pmStatusPanel == null) return;
 this.pmStatusPanel.setAttribute("status", this.kwButtonStatusOn);
 this.pmStatusPanel.tooltipText='Special delivery awaits for you';
}

StatusBarManager.prototype.setPMStatusOff = function()
{
 if (this.pmStatusPanel == null) return;
 this.pmStatusPanel.setAttribute("status", this.kwButtonStatusOff);
 this.pmStatusPanel.tooltipText='No messages';
}

StatusBarManager.prototype.setLRStatusBarPanel = function(statusBarPanel)
{
 this.lrStatusPanel = statusBarPanel;
 if (this.lrStatusPanel == null) return;
 //this.lrStatusPanel.setAttribute("status", "on");
}

StatusBarManager.prototype.setLRStatusOn = function()
{
 if (this.lrStatusPanel == null) return;
 this.lrStatusPanel.setAttribute("status", this.kwButtonStatusOn);
 this.lrStatusPanel.tooltipText='Someone is making a mess in your living room';
}

StatusBarManager.prototype.setLRStatusOff = function()
{
 if (this.lrStatusPanel == null) return;
 this.lrStatusPanel.setAttribute("status", this.kwButtonStatusOff);
  this.lrStatusPanel.tooltipText='Living room? More like \"Boring Room\" if you ask me';
}

StatusBarManager.prototype.hidePM = function(hidden) {
	if (this.pmStatusPanel)
		this.pmStatusPanel.setAttribute("hidden", hidden);
}

StatusBarManager.prototype.hideLR = function(hidden) {
	if (this.lrStatusPanel)
		this.lrStatusPanel.setAttribute("hidden", hidden);
}

StatusBarManager.prototype.hideKW = function(hidden) {
	if (this.kwStatusPanel)
		this.kwStatusPanel.setAttribute("hidden", hidden);
}

StatusBarManager.prototype.hideSale = function(hidden) {
	if (this.saleStatusPanel)
		this.saleStatusPanel.setAttribute("hidden", hidden);
}

StatusBarManager.prototype.hideStatusBar = function(hidden) {
	if (this.lrStatusPanel)
		this.lrStatusPanel.setAttribute("hidden", hidden);
	if (this.pmStatusPanel)
		this.pmStatusPanel.setAttribute("hidden", hidden);
	if (this.kwStatusPanel)
		this.kwStatusPanel.setAttribute("hidden", hidden);
	if (this.saleStatusPanel)
		this.saleStatusPanel.setAttribute("hidden", hidden);					
}

StatusBarManager.prototype.setSaleStatusBarPanel = function(statusBarPanel)
{
 this.saleStatusPanel = statusBarPanel;
}

StatusBarManager.prototype.setSaleStatusDisconnected = function()
{
 if (this.saleStatusPanel == null) return;
 this.saleStatusPanel.label="JellyFish";
 this.saleStatusPanel.tooltipText="Not Connected";
}

StatusBarManager.prototype.toggleSaleInfo = function() {
	//alert('sbm change');
	this.saleShowPercentOff = !this.saleShowPercentOff;
	this.monitorState(this.defaultSaleInterval);
}

StatusBarManager.prototype.isMonitoring = function() {
	if (this.saleIntervalId != null) {
		return true;
	}
	return false;
}

StatusBarManager.prototype.monitorState = function(interval)
{
	//alert('sb start monitor ' + interval);
	//alert('monitorState ' + this.saleStatusPanel);
	if (this.saleStatusPanel == null) return;
	if (this.saleIntervalId != null) {
		this.stopMonitorState();
		//alert('calling refreshSaleStatus');
		this.refreshSaleStatus(this);
		//alert('done calling refreshSaleStatus');
	}
	this.saleInterval = interval;
	this.saleIntervalId = setInterval(this.refreshSaleStatus, this.saleInterval, this);
	//this.refreshSaleStatus(this);
}

StatusBarManager.prototype.stopMonitorState = function()
{
	//alert('sb stop monitor');
	if (this.saleIntervalId != null) {
		clearInterval(this.saleIntervalId);
		this.saleIntervalId = null;
	}
	this.saleStatusPanel.label='JellyFish';
	this.saleStatusPanel.tooltipText='Not monitoring';
	//this.refreshSaleStatus(this);
}

StatusBarManager.prototype.refreshSaleStatus = function(handle)
{
	if (handle.saleStatusPanel == null && handle.saleIntervalId != null) {
		clearInterval(handle.saleIntervalId);
		return;
	}
	//alert('pre get state ' + swaksmack.SS_JF);
	//var state = swaksmack.alertManager.state;
	var state = swaksmack.SS_JF.getState();
	//alert('state ' + state);
	if (state == SMACK_STATE_NONGURUDEAL || state == SMACK_STATE_GURUGUESSMADE || state == SMACK_STATE_GURUGUESSMISSED) {
		try {
			var salelable = 'Deal';
			var saleTT = 'Deal';
			var retail = swaksmack.SmackShopDoc.getElementById('retailPrice');
			var item = swaksmack.SmackShopDoc.getElementById('productNameAnchor');
			if (handle.saleShowPercentOff == true) { 
				var cost = swaksmack.SmackShopDoc.getElementById('slider');
				salelable = cost.textContent;				
			}
			else {
				var cost = swaksmack.SmackShopDoc.getElementById('totalCost');
				salelable = cost.textContent;
			}
			saleTT = item.textContent + '   Retail: ' + retail.textContent;
			
			handle.saleStatusPanel.label=salelable;
			handle.saleStatusPanel.tooltipText=saleTT;
		
		}
		catch (x) {
			handle.saleStatusPanel.label="Product Error";
			handle.saleStatusPanel.tooltipText=x;
		}
		
	}
	/*
	else if (state == SMACK_STATE_GURUGUESSMADE || state == SMACK_STATE_GURUGUESSMISSED) {
		try {
			var salelable = 'Deal';
			var saleTT = 'Deal';
			var cost = swaksmack.SmackShopDoc.getElementById('totalCost');
			salelable = cost.textContent;
			var item = swaksmack.SmackShopDoc.getElementById('productNameAnchor');
			saleTT = item.textContent;

			handle.saleStatusPanel.label=salelable;
			handle.saleStatusPanel.tooltipText=saleTT;
		


		}
		catch (x) {
			handle.saleStatusPanel.label="Product Error";
			handle.saleStatusPanel.tooltipText=x;
		}
		
	}
	*/
	else if (state == SMACK_STATE_NONGURUDEALSOLD) {
	  handle.saleStatusPanel.label="Sold";
		handle.saleStatusPanel.tooltipText='Item sold';
	}
	else if (state == SMACK_STATE_OUTWITVOTE) {
		handle.saleStatusPanel.label="Outwit Vote";
		handle.saleStatusPanel.tooltipText='Vote early, vote often';
	}	
	else if (state == SMACK_STATE_OUTWITOPTIN) {
		handle.saleStatusPanel.label="Opt In";
		handle.saleStatusPanel.tooltipText='What are you waiting for?';
	}
	else if (state == SMACK_STATE_OUTWIT_VOTED) {
		handle.saleStatusPanel.label="Voted";
		handle.saleStatusPanel.tooltipText='I would have picked something else';
	}
	else if (state == SMACK_STATE_OUTWITOPTINENTERED) {
		handle.saleStatusPanel.label='You\'re in';
		handle.saleStatusPanel.tooltipText='Feelin Lucky?';
	}
	else if (state == SMACK_STATE_OUTWITCONTESTANTSELECTED) {
		handle.saleStatusPanel.label='Player Selected';
		handle.saleStatusPanel.tooltipText='If it wasn\'t me, I hope it was you';
		try {
			var pnid = swaksmack.SmackShopDoc.getElementById('playerName');
			if (pnid != null) {
				var nickname = swaksmack.SS_JF.getNickName();
				handle.saleStatusPanel.tooltipText=pnid.textContent;
				if (nickname == pnid.textContent) {
					handle.saleStatusPanel.label='You Are Selected';
				}
			}
		}
		catch (x){}
	}
	else if (state == SMACK_STATE_OUTWITRESULT) {
		handle.saleStatusPanel.label="Outwit Result";
		handle.saleStatusPanel.tooltipText='I will get the answer next time';
		var winner = swaksmack.SS_JF.isGuruWinner();
		if (winner) {
			handle.saleStatusPanel.label="Outwit Point";
			handle.saleStatusPanel.tooltipText='Nice guess';
		}
		else {
			var other = swaksmack.SS_JF.isContestWinner();
			if (other) {
				handle.saleStatusPanel.label="Outwit Result";
				handle.saleStatusPanel.tooltipText='At least someone got a point';
			}
		}
	}	
	else if (state == SMACK_STATE_OUTWITSELECTCONTESTANT) {
		handle.saleStatusPanel.label="Player Selection";
		handle.saleStatusPanel.tooltipText='Pick me, Pick me';
	}
	else if (state == SMACK_STATE_GURUGUESSENTER) {
		handle.saleStatusPanel.label="Guru Guess";
		handle.saleStatusPanel.tooltipText='Enter your Guru Guess';
	}
	else if (state == SMACK_STATE_GURUGUESSSOLD) {
		handle.saleStatusPanel.label="Sold";
		handle.saleStatusPanel.tooltipText='Item sold';
	}
	else if (state == SMACK_STATE_GURUGUESSWHEELSPIN) {
		handle.saleStatusPanel.label="Wheel Spin";
		handle.saleStatusPanel.tooltipText='The wheel on the smack goes round and round';
	}
  else if (state == SMACK_STATE_GURUGUESSWHEELSPINSTOPPED) {
		handle.saleStatusPanel.label="Wheel Stopped";
		handle.saleStatusPanel.tooltipText='Wheel stopped';
	}
	else if (state == SMACK_STATE_PREVIEW) {
		handle.saleStatusPanel.label="Preview";
		handle.saleStatusPanel.tooltipText='Waiting until next show...go do something';
		try {
			var lot = swaksmack.SmackShopDoc.getElementById('liveObjectTimer');
			handle.saleStatusPanel.label=lot.textContent;
		}
		catch (x){}
	}
	else if (state == SMACK_STATE_UNKNOWN) {
		//alert(state + " unknown " + swaksmack.SS_JF.getTypeIdentifier() + " " + swaksmack.SS_JF.getGuruGuessState());
		handle.saleStatusPanel.label="Waiting...";
		handle.saleStatusPanel.tooltipText='You may want to refresh the page.  State info: ' + state + ' ' + swaksmack.SS_JF.getTypeIdentifier() + ' ' + swaksmack.SS_JF.getGuruGuessState();
	} 
	else {
		//alert(state + " " + swaksmack.SS_JF.getTypeIdentifier() + " " + swaksmack.SS_JF.getGuruGuessState());
		handle.saleStatusPanel.label='Where am I? '+ state;
		handle.saleStatusPanel.tooltipText='Look, he goofed, PM Mindstorm and tell him of the problem: ' + state + ' ' + swaksmack.SS_JF.getTypeIdentifier() + ' ' + swaksmack.SS_JF.getGuruGuessState();
	}
	handle.oldState = state;
	//handle.saleIntervalId = setInterval(this.refreshSaleStatus, this.saleInterval, this);
	
}



